<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: orders_edit.php 77 2013-09-21 12:02:13Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 77 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:02:13 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     neommerce - www.neommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

define('DISCOUNT_MODULES', 'ot_discount, ot_payment');
define('FORMAT_NEGATIVE', '<strong style="color:#b20000">%s</strong>');

require(DIR_WS_CLASSES.'order.php');

if(!$_GET['oID'])
	$_GET['oID'] = $_POST['oID'];

$order = new order($_GET['oID']);

require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.price.php');
$price = new price($order->info['currency'], $order->info['status']);

require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.main.php');
$main = new main();

require_once (DIR_FS_INC.'inc.get_tax_class_id.php');
require_once (DIR_FS_INC.'inc.get_tax_rate.php');

require_once (DIR_FS_INC.'inc.oe_get_options_name.php');
require_once (DIR_FS_INC.'inc.oe_get_options_values_name.php');
require_once (DIR_FS_INC.'inc.oe_customer_infos.php');

switch($_GET['action']) {
	case 'address_edit' : // Adressbearbeitung
		$lang = $db->db_query("SELECT 
									languages_id 
								FROM 
									".TABLE_LANGUAGES." 
								WHERE 
									directory = '".$order->info['language']."'");

		$status = $db->db_query("SELECT 
									customers_status_name 
								FROM 
									".TABLE_CUSTOMERS_STATUS." 
								WHERE 
									customers_status_id = '".$_POST['customers_status']."' 
								AND 
									language_id = '".$lang->fields['languages_id']."' ");

		$sql_data_array = array('customers_vat_id' => $_POST['customers_vat_id'],
								'customers_status' => $_POST['customers_status'],
								'customers_status_name' => $status->fields['customers_status_name'],
								'customers_company' => $_POST['customers_company'],
								'customers_name' => $_POST['customers_firstname'].' '.$_POST['customers_lastname'],
								'customers_firstname' => $_POST['customers_firstname'],
								'customers_lastname' => $_POST['customers_lastname'],
								'customers_street_address' => $_POST['customers_street_address'],
								'customers_city' => $_POST['customers_city'],
								'customers_postcode' => $_POST['customers_postcode'],
								'customers_country' => $_POST['customers_country'],
								'customers_telephone' => $_POST['customers_telephone'],
								'customers_email_address' => $_POST['customers_email_address'],
								'delivery_company' => $_POST['delivery_company'],
								'delivery_name' => $_POST['delivery_firstname'].' '.$_POST['delivery_lastname'],
								'delivery_firstname' => $_POST['delivery_firstname'],
								'delivery_lastname' => $_POST['delivery_lastname'],
								'delivery_street_address' => $_POST['delivery_street_address'],
								'delivery_city' => $_POST['delivery_city'],
								'delivery_postcode' => $_POST['delivery_postcode'],
								'delivery_country' => $_POST['delivery_country'],
								'billing_company' => $_POST['billing_company'],
								'billing_name' => $_POST['billing_firstname'].' '.$_POST['billing_lastname'],
								'billing_firstname' => $_POST['billing_firstname'],
								'billing_lastname' => $_POST['billing_lastname'],
								'billing_street_address' => $_POST['billing_street_address'],
								'billing_city' => $_POST['billing_city'],
								'billing_postcode' => $_POST['billing_postcode'],
								'billing_country' => $_POST['billing_country'],
								'last_modified' => 'NOW()');

		$db->db_perform(TABLE_ORDERS, $sql_data_array, 'update', 'orders_id = \''.$_POST['oID'].'\'');

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=address&oID='.$_POST['oID']));
		break;

	case 'product_edit' : // Artikel bearbeiten
		$status = $db->db_query("SELECT 
										customers_status_show_price_tax 
									FROM 
										".TABLE_CUSTOMERS_STATUS." 
									WHERE 
										customers_status_id = '".$order->info['status']."'");

		$final_price = $_POST['products_price'] * $_POST['products_quantity'];
		
		$get = $db->db_query("SELECT 
									products_image
								FROM 
									".TABLE_PRODUCTS." 
								WHERE 
									products_id = '".$_POST['products_id']."' ");
		
		$sql_data_array = array('orders_id' => $_POST['oID'],
								'products_id' => $_POST['products_id'],
								'products_name' => $_POST['products_name'],
								'products_model' => $_POST['products_model'],
								'products_ean' => $_POST['products_ean'],
								'products_image' => ($get->fields['products_image'] !='' ? 'images/product_images/icon_images/'.$get->fields['products_image'] : ''),
								'products_price' => $_POST['products_price'],
								'products_discount_made' => '', 
								'final_price' => $final_price,
								'products_tax' => $_POST['products_tax'],
								'products_quantity' => $_POST['products_quantity'],
								'products_shipping_time'=> $main->getShippingStatusName($product->fields['products_shippingtime']),
								'allow_tax' => $status->fields['customers_status_show_price_tax']);

		$db->db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array, 'update', 'orders_products_id = \''.$_POST['opID'].'\'');

		if($_POST['products_quantity'] != 0) {
			$qty = $db->db_query("SELECT 
										products_quantity 
									FROM 
										".TABLE_ORDERS_PRODUCTS." 
									WHERE 
										orders_products_id = '".$_POST['products_id']."' ");
			if($_POST['products_quantity'] > $qty->fields['products_quantity']) {
				$dif = ($_POST['products_quantity']-$qty->fields['products_quantity']);
				$db->db_query("UPDATE ".TABLE_PRODUCTS." SET products_quantity = products_quantity-".$dif." WHERE products_id= ".$_POST['products_id']);

			} elseif($_POST['products_quantity'] < $qty->fields['products_quantity']) {
				$dif = ($qty->fields['products_quantity'] - $_POST['products_quantity']);
				$db->db_query("UPDATE ".TABLE_PRODUCTS." SET products_quantity = products_quantity+".$dif." WHERE products_id= ".$_POST['products_id']);
			}
		}

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=products&oID='.$_POST['oID']));
		break;

	case 'product_ins' : // Artikel einfügen

		$status = $db->db_query("SELECT 
										customers_status_show_price_tax 
									FROM 
										".TABLE_CUSTOMERS_STATUS." 
									WHERE 
										customers_status_id = '".$order->info['status']."'");

		$product = $db->db_query("SELECT 
										p.products_model, 
										p.products_ean,
										p.products_tax_class_id, 
										p.products_quantity,
										p.products_shippingtime,
										p.products_image,
										pd.products_name,
										b.brand_name
									FROM 
										".TABLE_PRODUCTS." p, 
										".TABLE_PRODUCTS_DESCRIPTION." pd,
										".TABLE_BRAND." b
									WHERE 
										p.products_id = '".$_POST['products_id']."' 
									AND 
										pd.products_id = p.products_id
									AND
										b.brand_id = p.brand_id
									AND 
										pd.language_id = '".$_SESSION['languages_id']."'");

		$c_info = oe_customer_infos($order->customer['ID']);
		$tax_rate = get_tax_rate($product->fields['products_tax_class_id'], $c_info['country_id'], $c_info['zone_id']);

		if(STOCK_ALLOW_CHECKOUT_DEACTIVATE == 'true')
			$qty = $_POST['products_quantity'];
		else {
			if($_POST['products_quantity'] > $product->fields['products_quantity']) {
				$qty = $product->fields['products_quantity'];
				$message_stack->add_session('Statt der geforderten Menge von '.$_POST['products_quantity'].' wurde der Restbestand von '.$product->fields['products_quantity'].' übernommen', 'error');
			} else
				$qty = $_POST['products_quantity'];
		}

		$Pprice = $price->GetPrice($_POST['products_id'], false, 1, $product->fields['products_tax_class_id'], '', '', $order->customer['ID']);

		$final_price = $Pprice * $_POST['products_quantity'];
		
		$sql_data_array = array('orders_id' => $_POST['oID'],
								'products_id' => $_POST['products_id'],
								'products_name' => $product->fields['brand_name'].' - '.$product->fields['products_model'].' - '.$product->fields['products_name'],
								'products_image' => ($product->fields['products_image'] !='' ? 'images/product_images/icon_images/'.$product->fields['products_image'] : 'images/no_image.jpg'),
								'products_price' => $Pprice,
								'products_discount_made' => '',
								'products_shipping_time' => $main->getShippingStatusName($product->fields['products_shippingtime']),
								'products_model' => $product->fields['products_model'],
								'products_ean' => $product->fields['products_ean'],
								'final_price' => $final_price,
								'products_tax' => $tax_rate,
								'products_quantity' => ($qty > 0 ? $qty : 1),
								'allow_tax' => $status->fields['customers_status_show_price_tax']);

		$db->db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array);

		if($_POST['products_quantity'] != 0)
	    	$db->db_query("UPDATE ".TABLE_PRODUCTS." SET products_quantity = products_quantity - ".$qty." WHERE products_id= ".$_POST['products_id']);

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=products&oID='.$_POST['oID']));
		break;


	case 'save_order' :

		/*********************************************************
		*
		*	alten Speicher leeren
		*/

		$db->db_query("DELETE FROM ".TABLE_ORDERS_RECALCULATE." WHERE orders_id = '".(int)$_POST['oID']."'");
		
		
		/*********************************************************
		*
		*	Zusatzinfo für Steuerberechnung ermitteln
		*/
		
		$lang = $db->db_query("SELECT 
									languages_id
	                    		FROM 
	                    			".TABLE_LANGUAGES."
	                    		WHERE 
	                    			directory = '".$order->info['language']."'");
		
		$status = $db->db_query("SELECT 
										customers_status_show_price_tax,
										customers_status_add_tax_ot
									FROM 
										".TABLE_CUSTOMERS_STATUS."
									WHERE 
										customers_status_id = '".$order->info['status']."'
									AND 
										language_id ='".(int)$lang->fields['languages_id']."'");
		
		/*********************************************************
		*
		*	Produkte addieren
		*/

		$products = $db->db_query("SELECT 
										SUM(final_price) as subtotal_final 
									FROM 
										".TABLE_ORDERS_PRODUCTS." 
									WHERE 
										orders_id = '".(int)$_POST['oID']."'");

		$subtotal_final = $products->fields['subtotal_final'];
		$subtotal_text = $price->format($subtotal_final, true);
		
		$db->db_query("UPDATE 
							".TABLE_ORDERS_TOTAL." 
						SET 
							text = '".$subtotal_text."', 
							value = '".$subtotal_final."' 
						WHERE 
							orders_id = '".(int)$_POST['oID']."' 
						AND 
							class = 'ot_subtotal' ");


		/*********************************************************
		*
		*	Produkte einzeln berechnen und Steuern ermitteln
		*/

		$products = $db->db_query("SELECT 
										final_price,
										products_tax, 
										allow_tax 
									FROM 
										".TABLE_ORDERS_PRODUCTS." 
									WHERE 
										orders_id = '".(int)$_POST['oID']."'");

		while(!$products->EOF) {
		
			$tax_rate = $products->fields['products_tax'];
			
			if($products->fields['allow_tax'] == '1') {
				$bprice = $products->fields['final_price'];
				$nprice = $price->RemoveTax($bprice, $tax_rate);
				$tax = $price->calcTax($bprice, $tax_rate);

			} else {
				$nprice = $products->fields['final_price'];
				$bprice = $price->AddTax($nprice, $tax_rate);
				$tax = $price->calcTax($bprice, $tax_rate);
			}
			
			$sql_data_array = array ('orders_id' => (int)($_POST['oID']),
					                 'n_price' => $nprice,
					                 'b_price' => $bprice,
					                 'tax' => $tax,
					                 'class' => 'products',
					                 'tax_rate' => $products->fields['products_tax']);

			$db->db_perform(TABLE_ORDERS_RECALCULATE, $sql_data_array);
			$products->MoveNext();
		}
		
		
		/*********************************************************
		*
		*	Steuersätze addieren und ablegen
		*/

		$tax = $db->db_query("SELECT
									tax_rate, 
									SUM(tax) AS tax_value
								FROM 
									".TABLE_ORDERS_RECALCULATE."
								WHERE 
									orders_id = '".(int)$_POST['oID']."'
								AND 
									class = 'products'
								GROUP BY 
									tax_rate");


		while (!$tax->EOF) {
			$sql_data_array = array ('orders_id' => (int)($_POST['oID']),
					                 'tax' => $tax->fields['tax_value'],
					                 'tax_rate' => $tax->fields['tax_rate'],
					                 'class' => 'ot_tax');

			$db->db_perform(TABLE_ORDERS_RECALCULATE, $sql_data_array);
			$tax->MoveNext();
		}

		/*********************************************************
		*
		*	restliche Module berechnen und ggf. Steuern berechnen
		*/

		$module = $db->db_query("SELECT 
										value, 
										class
									FROM 
										".TABLE_ORDERS_TOTAL."
									WHERE 
										orders_id = '".(int)$_POST['oID']."'
									AND 
										class!='ot_total'
									AND 
										class!='ot_subtotal_no_tax'
									AND 
										class!='ot_tax'
									AND
										class!='ot_total_netto'
									AND 
										class!='ot_subtotal'");
		
		$discount_modules = array_map('trim',explode(",", DISCOUNT_MODULES)); // Rabattmodule

		while(!$module->EOF) {
			$module_name = str_replace('ot_', '', $module->fields['class']);
			if(!in_array($module->fields['class'], $discount_modules)) {
				$module_tax_class = '0';
				if($module_name != 'shipping' && defined('MODULE_ORDER_TOTAL_'.strtoupper($module_name).'_TAX_CLASS'))
					$module_tax_class = constant('MODULE_ORDER_TOTAL_'.strtoupper($module_name).'_TAX_CLASS');
				else {
					$module_tmp_name = end(explode('_', $order->info['shipping_class']));
					if($module_tmp_name != 'selfpickup' && $module_tmp_name != 'free' && defined('MODULE_SHIPPING_'.strtoupper($module_tmp_name).'_TAX_CLASS'))
						$module_tax_class = constant('MODULE_SHIPPING_'.strtoupper($module_tmp_name).'_TAX_CLASS');
				}

			} else
				$module_tax_class = '0';
			
			$c_info = get_c_infos($order->customer['ID'], trim($order->delivery['country_iso_2']));
			$module_tax_rate = get_tax_rate($module_tax_class, $c_info['country_id'], $c_info['zone_id']);
			
			if($status->fields['customers_status_show_price_tax'] == 1) {
				$module_b_price = $module->fields['value'];
				if($module_tax_class == '0') {
					$module_n_price = $module->fields['value'];
					$module_tax = 0;

				} else {
					$module_n_price = $price->RemoveTax($module_b_price, $module_tax_rate);
					$module_tax = $price->calcTax($module_b_price, $module_tax_rate);
				}

			} else {
				$module_n_price = $module->fields['value'];
				$module_b_price = $price->AddTax($module_n_price, $module_tax_rate);
				$module_tax = $price->calcTax($module_b_price, $module_tax_rate);
			}
			
			if($module_name != 'shipping' && $module_name != 'cod_fee' && $module_tax_rate == 0)
				$module_tax = calculate_tax($module->fields['value']);
			
			$sql_data_array = array('orders_id' => (int)($_POST['oID']),
									'n_price' => $module_n_price,
									'b_price' => $module_b_price,
									'tax' => $module_tax,
									'class' => $module->fields['class'],
									'tax_rate' => $module_tax_rate);

			$db->db_perform(TABLE_ORDERS_RECALCULATE, $sql_data_array);
			$module->MoveNext();
		}
		
		/*********************************************************
		*
		*	Summe netto: berechnen
		*/

		$netto = $db->db_query("SELECT 
										SUM(n_price) as netto
									FROM 
										".TABLE_ORDERS_RECALCULATE."
									WHERE 
										orders_id = '".(int)$_POST['oID']."'");
										
		$db->db_query("UPDATE 
							".TABLE_ORDERS_TOTAL."
						SET 
							value = '".$netto->fields['netto']."',
							text = '".$price->format($netto->fields['netto'], true)."'
						WHERE 
							orders_id = '".(int)$_POST['oID']."'
						AND 
							class = 'ot_total_netto'");						
										
										
		/*********************************************************
		*
		*	Steuerbeträge der Zusatzmodule suchen und ggf. ot_tax
		*	updaten
		*/

		$newtax = $db->db_query("SELECT 
										tax_rate 
									FROM 
										".TABLE_ORDERS_RECALCULATE." 
									WHERE 
										orders_id = '".(int)$_POST['oID']."' 
									AND 
										class = 'ot_tax' 
									GROUP BY 
										tax_rate");

		while(!$newtax->EOF) {
			$newtax_array = $db->db_query("SELECT 
												SUM(tax) AS new_tax_value
											FROM 
												".TABLE_ORDERS_RECALCULATE."
											WHERE 
												orders_id = '".(int)$_POST['oID']."'
											AND 
												tax_rate > 0
											AND 
												tax_rate = '".$newtax->fields['tax_rate']."'");

			if($newtax_array->fields['new_tax_value'] > 0) {
				$db->db_query("UPDATE 
									".TABLE_ORDERS_RECALCULATE."
								SET 
									tax = '".$newtax_array->fields['new_tax_value']."'
								WHERE 
									orders_id = '".(int)$_POST['oID']."'
								AND 
									tax_rate = '".$newtax->fields['tax_rate']."'
								AND 
									class = 'ot_tax'");
			}
			$newtax->MoveNext();
		}
		
		/*********************************************************
		*
		*	Gesamtsumme Netto ohne Steuern oder mit?
		*/
 
		$check_no_tax_value = $db->db_query("SELECT 
												count(*) AS count 
											FROM 
												".TABLE_ORDERS_TOTAL." 
											WHERE 
												orders_id = '".(int)$_POST['oID']."' 
											AND 
												class = 'ot_subtotal_no_tax'");
		
		if((int)$check_no_tax_value->fields['count'] > 0) {
			include(DIR_FS_LANGUAGES.$order->info['language'].'/modules/order_total/ot_subtotal_no_tax.php');

			$subtotal_no_tax_value = $db->db_query("select SUM(n_price) as subtotal_no_tax_value from ".TABLE_ORDERS_RECALCULATE." where orders_id = '".(int)$_POST['oID']."'");

			$subtotal_no_tax_final = $subtotal_no_tax_value->fields['subtotal_no_tax_value'];
			$subtotal_no_tax_text = $price->format($subtotal_no_tax_final, true);
			
			$sql_data_array = array('title' => MODULE_ORDER_TOTAL_SUBTOTAL_NO_TAX_TITLE. ':',
					                'text'  => $subtotal_no_tax_text,
					                'value' => $subtotal_no_tax_final,
					                'sort_order' => MODULE_ORDER_TOTAL_SUBTOTAL_NO_TAX_SORT_ORDER);
			
			$db->db_perform(TABLE_ORDERS_TOTAL, $sql_data_array, 'update', "orders_id = '".(int)($_POST['oID'])."' and class = 'ot_subtotal_no_tax'");
		
		} else {
			if($status->fields['customers_status_show_price_tax'] == 0 && $status->fields['customers_status_add_tax_ot'] == 1 && MODULE_ORDER_TOTAL_SUBTOTAL_NO_TAX_STATUS){
			
				include (DIR_FS_LANGUAGES.$order->info['language'].'/modules/order_total/ot_subtotal_no_tax.php');
				
				$subtotal_no_tax_value = $db->db_query("select SUM(n_price) as subtotal_no_tax_value from ".TABLE_ORDERS_RECALCULATE." where orders_id = '".(int)$_POST['oID']."'");

				$subtotal_no_tax_final = $subtotal_no_tax_value->fields['subtotal_no_tax_value'];
				$subtotal_no_tax_text = $price->format($subtotal_no_tax_final, true);
				
				$sql_data_array = array ('orders_id' => (int)($_POST['oID']),
						                  'title' => MODULE_ORDER_TOTAL_SUBTOTAL_NO_TAX_TITLE. ':',
						                  'text' => $subtotal_no_tax_text,
						                  'value' => $subtotal_no_tax_final,
						                  'class' => 'ot_subtotal_no_tax',
						                  'sort_order' => MODULE_ORDER_TOTAL_SUBTOTAL_NO_TAX_SORT_ORDER);
				$db->db_perform(TABLE_ORDERS_TOTAL, $sql_data_array);
			}
		}
		
		
		if(!MODULE_ORDER_TOTAL_SUBTOTAL_NO_TAX_STATUS || $status->fields['customers_status_show_price_tax'] == 1){
			$db->db_query("DELETE FROM 
								".TABLE_ORDERS_TOTAL." 
							WHERE 
								orders_id = '".(int)($_POST['oID'])."' 
							AND 
								class='ot_subtotal_no_tax'");	
		}

		$ust = $db->db_query("SELECT 
									tax_rate, 
									SUM(tax) AS tax_value_new
								FROM 
									".TABLE_ORDERS_RECALCULATE."
								WHERE 
									orders_id = '".(int)$_POST['oID']."'
								AND 
									tax !='0'
								AND 
									class = 'ot_tax'
								GROUP BY 
									tax_rate DESC");
		
		while(!$ust->EOF) {
			$ust_desc = $db->db_query("select tax_description from ".TABLE_TAX_RATES." where tax_rate = '".$ust->fields['tax_rate']."'");

			$title = $ust_desc->fields['tax_description'];
			$tax_info = '';
			if($status->fields['customers_status_show_price_tax'] == 1)
				$tax_info = TAX_ADD_TAX;
			if($status->fields['customers_status_show_price_tax'] == 0)
				$tax_info = TAX_NO_TAX;
			$title = $tax_info.$title.':';
			
			if($ust->fields['tax_value_new']) {
				$text = $price->format($ust->fields['tax_value_new'], true);
	
				$sql_data_array = array('orders_id' => (int)$_POST['oID'],
						                  'title' => $title,
						                  'text' => $text,
						                  'value' => $ust->fields['tax_value_new'],
						                  'sort_order' => MODULE_ORDER_TOTAL_TAX_SORT_ORDER);
	
				$db->db_perform(TABLE_ORDERS_TOTAL, $sql_data_array, 'update', "orders_id = '".(int)$_POST['oID']."' AND class = 'ot_tax'");
			}
			$ust->MoveNext();
		}

		if($status->fields['customers_status_show_price_tax'] == 0 && $status->fields['customers_status_add_tax_ot'] == 0){
			$db->db_query("DELETE FROM 
								".TABLE_ORDERS_TOTAL." 
							WHERE
								orders_id = '".(int)($_POST['oID'])."'
							AND
								class='ot_tax'");
		}
			

		$add_tax = 0;
		$Tprice = 'b_price';
		if($status->fields['customers_status_show_price_tax'] == 0 && $status->fields['customers_status_add_tax_ot'] == 1) {
			$tax = $db->db_query("SELECT 
										SUM(value) AS value 
									FROM 
										".TABLE_ORDERS_TOTAL." 
									WHERE 
										orders_id = '".(int)($_POST['oID'])."' 
									AND 
										class='ot_tax'");
			$add_tax = $tax->fields['value'];
			$Tprice = 'n_price';
		}
		
		$total = $db->db_query("SELECT 
									SUM(".$Tprice.") AS value 
								FROM 
									".TABLE_ORDERS_RECALCULATE." 
								WHERE 
									orders_id = '".(int)$_POST['oID']."'");

		$total_final = $total->fields['value'] + $add_tax;
		$total_text = $price->format($total_final, true);
		
		$db->db_query("UPDATE 
							".TABLE_ORDERS_TOTAL."
						SET 
							text = '".$total_text."',
							value = '".$total_final."'
						WHERE 
							orders_id = '".(int)$_POST['oID']."'
						AND 
							class = 'ot_total'");

		$db->db_query("delete from ".TABLE_ORDERS_RECALCULATE." where orders_id = '".$_POST['oID']."'");
		
		redirect(href_link(FILENAME_ORDERS, 'action=edit&oID='.(int)$_POST['oID']));
		break;

	case 'product_option_edit' : // Produkt Optionen bearbeiten
		$sql_data_array = array('products_options' => $_POST['products_options'], 
								'products_options_values' => $_POST['products_options_values'], 
								'options_values_price' => $_POST['options_values_price']);

		$update_sql_data = array ('price_prefix' => $_POST['prefix']);
		$sql_data_array = array_merge($sql_data_array, $update_sql_data);
		$db->db_perform(TABLE_ORDERS_PRODUCTS_ATTRIBUTES, $sql_data_array, 'update', 'orders_products_attributes_id = \''.$_POST['opAID'].'\'');

		$products = $db->db_query("SELECT 
										op.products_id, 
										op.products_quantity, 
										p.products_tax_class_id 
									FROM 
										".TABLE_ORDERS_PRODUCTS." op, 
										".TABLE_PRODUCTS." p 
									WHERE 
										op.orders_products_id = '".$_POST['opID']."' 
									AND 
										op.products_id = p.products_id");

		$products_a = $db->db_query("SELECT 
											options_values_price, 
											price_prefix 
										FROM 
											".TABLE_ORDERS_PRODUCTS_ATTRIBUTES." 
										WHERE 
											orders_products_id = '".$_POST['opID']."'");
		while($products_a->EOF) {
			$ov_price += $products_a->fields['price_prefix'].$products_a->fields['options_values_price'];
			$products_a->MoveNext();
		}

		$products_old_price = $price->GetPrice($products->fields['products_id'], false, $products->fields['products_quantity'], '', '', '', $order->customer['ID']);

		$options_values_price = ($ov_price.$_POST['prefix'].$_POST['options_values_price']);
		$products_price = ($products_old_price + $options_values_price);

		$price = $price->GetPrice($products->fields['products_id'], false, $products->fields['products_quantity'], $products->fields['products_tax_class_id'], $products_price, '', $order->customer['ID']);

		$final_price = $price * $products->fields['products_quantity'];

		$sql_data_array = array ('products_price' => $price);
		$update_sql_data = array ('final_price' => $final_price);
		$sql_data_array = array_merge($sql_data_array, $update_sql_data);
		$db->db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array, 'update', 'orders_products_id = \''.$_POST['opID'].'\'');

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=options&oID='.$_POST['oID'].'&pID='.$products->fields['products_id'].'&opID='.$_POST['opID']));
		break;

	case 'product_option_ins' : // Produkt Optionen einfügen
		$products_attributes = $db->db_query("SELECT 
													options_id, 
													options_values_id, 
													options_values_price, 
													price_prefix 
												FROM 
													".TABLE_PRODUCTS_ATTRIBUTES." 
												WHERE 
													products_attributes_id = '".$_POST['aID']."'");

		$products_options = $db->db_query("SELECT 
												products_options_name 
											FROM 
												".TABLE_PRODUCTS_OPTIONS." 
											WHERE 
												products_options_id = '".$products_attributes->fields['options_id']."' 
											AND 
												language_id = '".$_SESSION['languages_id']."'");

		$products_options_values = $db->db_query("SELECT 
														products_options_values_name 
													FROM 
														".TABLE_PRODUCTS_OPTIONS_VALUES." 
													WHERE 
														products_options_values_id = '".$products_attributes->fields['options_values_id']."' 
													AND 
														language_id = '".$_SESSION['languages_id']."'");

		$sql_data_array = array('orders_id' => $_POST['oID'], 
								'orders_products_id' => $_POST['opID'], 
								'products_options' => $products_options->fields['products_options_name'], 
								'products_options_values' => $products_options_values->fields['products_options_values_name'], 
								'options_values_price' => $products_attributes->fields['options_values_price']);

		$insert_sql_data = array ('price_prefix' => $products_attributes->fields['price_prefix']);
		$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
		$db->db_perform(TABLE_ORDERS_PRODUCTS_ATTRIBUTES, $sql_data_array);

		$products = $db->db_query("SELECT 
										op.products_id, 
										op.products_quantity, 
										p.products_tax_class_id 
									FROM 
										".TABLE_ORDERS_PRODUCTS." op, 
										".TABLE_PRODUCTS." p 
									WHERE 
										op.orders_products_id = '".$_POST['opID']."' 
									AND 
										op.products_id = p.products_id");

		$products_a = $db->db_query("SELECT 
										options_values_price, 
										price_prefix 
									FROM 
										".TABLE_ORDERS_PRODUCTS_ATTRIBUTES." 
									WHERE 
										orders_products_id = '".$_POST['opID']."'");
		while (!$products_a->EOF) {
			$options_values_price += $products_a->fields['price_prefix'].$products_a->fields['options_values_price'];
			$products_a->moveNext();
		}

		if(DOWNLOAD_ENABLED == 'true') {
			$attributes_values = $db->db_query("SELECT 
													popt.products_options_name,
													poval.products_options_values_name,
													pa.options_values_price,
													pa.price_prefix,
													pad.products_attributes_maxdays,
													pad.products_attributes_maxcount,
													pad.products_attributes_filename
												FROM 
													".TABLE_PRODUCTS_OPTIONS." popt, 
													".TABLE_PRODUCTS_OPTIONS_VALUES." poval, 
													".TABLE_PRODUCTS_ATTRIBUTES." pa
													LEFT JOIN 
														".TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD." pad
													ON 
														pa.products_attributes_id=pad.products_attributes_id
												WHERE 
													pa.products_id = '".$products->fields['products_id']."'
												AND 
													pa.options_id = '".$products_attributes['options_id']."'
												AND 
													pa.options_id = popt.products_options_id
												AND 
													pa.options_values_id = '".$products_attributes['options_values_id']."'
												AND 
													pa.options_values_id = poval.products_options_values_id
												AND 
													popt.language_id = '".$_SESSION['languages_id']."'
												AND 
													poval.language_id = '".$_SESSION['languages_id']."'");

			if(isset($attributes_values->fields['products_attributes_filename']) && not_null($attributes_values->fields['products_attributes_filename'])) {
				$sql_data_array = array('orders_id' => $_POST['oID'], 
										'orders_products_id' => $_POST['opID'], 
										'orders_products_filename' => $attributes_values->fields['products_attributes_filename'], 
										'download_maxdays' => $attributes_values->fields['products_attributes_maxdays'], 
										'download_count' => $attributes_values->fields['products_attributes_maxcount']);

				$db->db_perform(TABLE_ORDERS_PRODUCTS_DOWNLOAD, $sql_data_array);
			}
		}

		$products_old_price = $price->GetPrice($products->fields['products_id'], false, $products->fields['products_quantity'], '', '', '', $order->customer['ID']);

		$products_price = ($products_old_price + $options_values_price);

		$price = $price->GetPrice($products->fields['products_id'], false, $products->fields['products_quantity'], $products->fields['products_tax_class_id'], $products_price, '', $order->customer['ID']);

		$final_price = $price * $products->fields['products_quantity'];

		$sql_data_array = array ('products_price' => $price);
		$update_sql_data = array ('final_price' => $final_price);
		$sql_data_array = array_merge($sql_data_array, $update_sql_data);
		$db->db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array, 'update', 'orders_products_id = \''.$_POST['opID'].'\'');

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=options&oID='.$_POST['oID'].'&pID='.$products->fields['products_id'].'&opID='.$_POST['opID']));
		break;

	case 'payment_edit' : // Zahlung Anfang
		$sql_data_array = array ('payment_method' => $_POST['payment'], 'payment_class' => $_POST['payment'],);
		$db->db_perform(TABLE_ORDERS, $sql_data_array, 'update', "orders_id = '".$_POST['oID']."'");

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=other&oID='.$_POST['oID']));
		break;

	case 'shipping_edit' : // Versandkosten
		$module = $_POST['shipping'].'.php';
		require (DIR_FS_LANGUAGES.$order->info['language'].'/modules/shipping/'.$module);
		$shipping_text = constant(MODULE_SHIPPING_.strtoupper($_POST['shipping'])._TEXT_TITLE);
		$shipping_class = $_POST['shipping'].'_'.$_POST['shipping'];

		$text = $price->format($_POST['value'], true);

		$sql_data_array = array('orders_id' => $_POST['oID'], 
								'title' => $shipping_text, 
								'text' => $text, 
								'value' => $_POST['value'], 
								'class' => 'ot_shipping');

		$check_shipping = $db->db_query("SELECT 
											class 
										FROM 
											".TABLE_ORDERS_TOTAL." 
										WHERE 
											orders_id = '".$_POST['oID']."' 
										AND 
											class = 'ot_shipping'");
		if($check_shipping->_numOfRows)
			$db->db_perform(TABLE_ORDERS_TOTAL, $sql_data_array, 'update', "orders_id = '".$_POST['oID']."' AND class = 'ot_shipping'");
		else
			$db->db_perform(TABLE_ORDERS_TOTAL, $sql_data_array);

		$sql_data_array = array('shipping_method' => $shipping_text, 
								'shipping_class' => $shipping_class);
								
		$db->db_perform(TABLE_ORDERS, $sql_data_array, 'update', "orders_id = '".$_POST['oID']."'");

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=other&oID='.$_POST['oID']));
		break;

	case 'ot_edit' : // OT Modul
		$check_total = $db->db_query("SELECT 
											orders_total_id 
										FROM 
											".TABLE_ORDERS_TOTAL." 
										WHERE 
											orders_id = '".$_POST['oID']."' 
										AND 
											class = '".$_POST['class']."'");
		if($check_total->_numOfRows) {
			$text = $price->format($_POST['value'], true);

			$sql_data_array = array('title' => $_POST['title'], 
									'text' => $text, 
									'value' => $_POST['value'],);
			$db->db_perform(TABLE_ORDERS_TOTAL, $sql_data_array, 'update', 'orders_total_id = \''.$check_total->fields['orders_total_id'].'\'');

		} else {

			$text = $price->format($_POST['value'], true);

			$sql_data_array = array('orders_id' => $_POST['oID'], 
									'title' => $_POST['title'], 
									'text' => $text, 
									'value' => $_POST['value'], 
									'class' => $_POST['class'], 
									'sort_order' => $_POST['sort_order'],);

			$db->db_perform(TABLE_ORDERS_TOTAL, $sql_data_array);
		}

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=other&oID='.$_POST['oID']));
		break;
	
	case 'ot_add':
		
		$products = $db->db_query("SELECT 
										final_price,
										products_tax, 
										allow_tax 
									FROM 
										".TABLE_ORDERS_PRODUCTS." 
									WHERE 
										orders_id = '".(int)$_POST['oID']."'");

		while(!$products->EOF) {
		
			$tax_rate = $products->fields['products_tax'];
			$with_tax = $products->fields['allow_tax'];
			
			if($with_tax == '1') {
				$bprice = $products->fields['final_price'];
				$nprice = $price->RemoveTax($bprice, $tax_rate);
				$value += $price->calcTax($bprice, $tax_rate);
			} else {
				$nprice = $products->fields['final_price'];
				$bprice = $price->AddTax($nprice, $tax_rate);
				$value += $price->calcTax($bprice, $tax_rate);
			}
			
			$products->MoveNext();
		}
		
		switch($_POST['name']) {
			case 'ot_tax':
				$g = $db->db_query("SELECT
										tax_description
									FROM
										".DB_PREFIX."tax_rates
									WHERE
										tax_rate = '".$tax_rate."'");
				$title = ($with_tax == 1 ? TAX_ADD_TAX : TAX_NO_TAX).$g->fields['tax_description'];
				
				break;
		}
		// constant(MODULE_ORDER_TOTAL_.strtoupper($name)._TITLE)
		require(DIR_FS_LANGUAGES.$order->info['language'].'/modules/order_total/'.$_POST['name'].'.php');	
		$name = str_replace('ot_', '', $_POST['name']);
		
		$sql_data_array = array('orders_id' => (int)($_POST['oID']),
								'title' => $title,
								'text' => $price->format($value, true),
								'value' => $value,
								'class' => $_POST['name'],
								'sort_order' => constant(MODULE_ORDER_TOTAL_.strtoupper($name)._SORT_ORDER));
		$db->db_perform(TABLE_ORDERS_TOTAL, $sql_data_array);
		
		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=other&oID='.$_POST['oID']));
		break;
	
	case 'lang_edit' : // Sprachupdate
		$lang = $db->db_query("SELECT 
									languages_id, 
									name, 
									directory 
								FROM 
									".TABLE_LANGUAGES." 
								WHERE 
									languages_id = '".$_POST['lang']."'");

		$order_products = $db->db_query("SELECT
											orders_products_id, 
											products_id 
										FROM 
											".TABLE_ORDERS_PRODUCTS." 
										WHERE 
											orders_id = '".$_POST['oID']."'");
		
		while (!$order_products->EOF) {

			$products = $db->db_query("SELECT 
											products_name 
										FROM 
											".TABLE_PRODUCTS_DESCRIPTION." 
										WHERE 
											products_id = '".$order_products->fields['products_id']."' 
										AND 
											language_id = '".$_POST['lang']."' ");

			$sql_data_array = array ('products_name' => $products->fields['products_name']);
			$db->db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array, 'update', 'orders_products_id  = \''.$order_products->fields['orders_products_id'].'\'');
			$order_products->MoveNext();
		}

		$order_total = $db->db_query("SELECT 
											orders_total_id, 
											title, 
											class 
										FROM 
											".TABLE_ORDERS_TOTAL." 
										WHERE 
											orders_id = '".$_POST['oID']."'");
		
		while (!$order_total->EOF) {
			require(DIR_FS_LANGUAGES.$lang['directory'].'/modules/order_total/'.$order_total->fields['class'].'.php');
			$name = str_replace('ot_', '', $order_total->fields['class']);
			$text = constant(MODULE_ORDER_TOTAL_.strtoupper($name)._TITLE);

			$sql_data_array = array ('title' => $text);
			$db->db_perform(TABLE_ORDERS_TOTAL, $sql_data_array, 'update', 'orders_total_id  = \''.$order_total->fields['orders_total_id'].'\'');
			$order_total->MoveNext();
		}

		$sql_data_array = array ('language' => $lang->fields['directory']);
		$db->db_perform(TABLE_ORDERS, $sql_data_array, 'update', 'orders_id  = \''.$_POST['oID'].'\'');

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=other&oID='.$_POST['oID']));
		break;

	case 'curr_edit' : // Währungswechsel
		$curr = $db->db_query("SELECT 
									currencies_id, title, code, value 
								FROM 
									".TABLE_CURRENCIES." 
								WHERE 
									currencies_id = '".$_POST['currencies_id']."' ");

		$old_curr = $db->db_query("SELECT 
										currencies_id, title, code, value 
									FROM 
										".TABLE_CURRENCIES." 
									WHERE 
										code = '".$_POST['old_currency']."' ");

		$sql_data_array = array('currency' => $curr->fields['code'], 
								'currency_value' => $curr->fields['value']);
		$db->db_perform(TABLE_ORDERS, $sql_data_array, 'update', 'orders_id  = \''.$_POST['oID'].'\'');

		// Produkte
		$order_products = $db->db_query("SELECT 
												orders_products_id, 
												products_id, 
												products_price, 
												final_price 
											FROM 
												".TABLE_ORDERS_PRODUCTS." 
											WHERE 
												orders_id = '".$_POST['oID']."'");
		while (!$order_products->EOF) {

			if($old_curr->fields['code'] == DEFAULT_CURRENCY) {
				$price = new Price($curr->fields['code'], $order->info['status']);
				$products_price = $price->GetPrice($order_products->fields['products_id'], false, '', '', $order_products->fields['products_price'], '', $order->customer['ID']);
				$final_price = $price->GetPrice($order_products->fields['products_id'], false, '', '', $order_products->fields['final_price'], '', $order->customer['ID']);
			} else {
				$price = new Price($old_curr->fields['code'], $order->info['status']);
				$p_price = $price->RemoveCurr($order_products->fields['products_price']);
				$f_price = $price->RemoveCurr($order_products->fields['final_price']);
				$price = new Price($curr->fields['code'], $order->info['status']);
				$products_price = $price->GetPrice($order_products->fields['products_id'], false, '', '', $p_price, '', $order->customer['ID']);
				$final_price = $price->GetPrice($order_products->fields['products_id'], false, '', '', $f_price, '', $order->customer['ID']);
			}
			$sql_data_array = array('products_price' => $products_price, 
									'final_price' => $final_price);

			$db->db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array, 'update', 'orders_products_id  = \''.$order_products->fields['orders_products_id'].'\'');
			$order_products->MoveNext();
		};

		$order_total = $db->db_query("SELECT 
											orders_total_id, 
											value 
										FROM 
											".TABLE_ORDERS_TOTAL." 
										WHERE 
											orders_id = '".$_POST['oID']."'");
		
		while (!$order_total->EOF) {
			if($old_curr['code'] == DEFAULT_CURRENCY) {
				$price = new Price($curr->fields['code'], $order->info['status']);
				$value = $price->GetPrice('', false, '', '', $order_total->fields['value'], '', $order->customer['ID']);

			} else {
				$price = new Price($old_curr->fields['code'], $order->info['status']);
				$nvalue = $price->RemoveCurr($order_total->fields['value']);

				$price = new Price($curr->fields['code'], $order->info['status']);
				$value = $price->GetPrice('', false, '', '', $nvalue, '', $order->customer['ID']);
			}

			$text = $text = $price->format($value, true);
			$sql_data_array = array ('text' => $text, 'value' => $value);
			$db->db_perform(TABLE_ORDERS_TOTAL, $sql_data_array, 'update', 'orders_total_id  = \''.$order_total->fields['orders_total_id'].'\'');
			$order_total->MoveNext();
		}

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=other&oID='.$_POST['oID']));
		break;

	case 'product_delete' : // Löschen eines Artikels aus der Bestellung

		$db->db_query("DELETE FROM
							".TABLE_ORDERS_PRODUCTS_ATTRIBUTES."
						WHERE
							orders_products_id = '".$_POST['opID']."'");

		$qty = $db->db_query("SELECT 
									products_quantity, 
									products_id 
								FROM 
									".TABLE_ORDERS_PRODUCTS." 
								WHERE 
									orders_products_id = '".$_POST['opID']."' ");

		$db->db_query("DELETE FROM
							".TABLE_ORDERS_PRODUCTS."
						WHERE
							orders_id = '".$_POST['oID']."'
						AND
							orders_products_id = '".$_POST['opID']."'");

		$db->db_query("UPDATE 
							products 
						SET 
							products_quantity = products_quantity+".$qty->fields['products_quantity']." 
						WHERE 
							products_id = '".$qty->fields['products_id']."' ");

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=products&oID='.$_POST['oID']));
		break;

	case 'product_option_delete' : // Löschen einer Artikeloption aus der Bestellung
		$db->db_query("DELETE FROM ".TABLE_ORDERS_PRODUCTS_ATTRIBUTES." WHERE orders_products_attributes_id = '".$_POST['opAID']."'");

		$products = $db->db_query("SELECT 
										op.products_id, 
										op.products_quantity, 
										p.products_tax_class_id 
									FROM 
										".TABLE_ORDERS_PRODUCTS." op, 
										".TABLE_PRODUCTS." p 
									WHERE 
										op.orders_products_id = '".$_POST['opID']."' 
									AND 
										op.products_id = p.products_id");

		$products_a = $db->db_query("SELECT 
										options_values_price, 
										price_prefix 
									FROM 
										".TABLE_ORDERS_PRODUCTS_ATTRIBUTES." 
									WHERE 
										orders_products_id = '".$_POST['opID']."'");
		while (!$products_a->EOF) {
			$options_values_price += $products_a->fields['price_prefix'].$products_a->fields['options_values_price'];
			$products_a->MoveNext();
		}

		$products_old_price = $price->GetPrice($products->fields['products_id'], false, $products->fields['products_quantity'], '', '', '', $order->customer['ID']);

		$products_price = ($products_old_price + $options_values_price);

		$price = $price->GetPrice($products->fields['products_id'], false, $products->fields['products_quantity'], $products->fields['products_tax_class_id'], $products_price, '', $order->customer['ID']);

		$final_price = $price * $products->fields['products_quantity'];

		$sql_data_array = array ('products_price' => $price);
		$update_sql_data = array ('final_price' => $final_price);
		$sql_data_array = array_merge($sql_data_array, $update_sql_data);
		$db->db_perform(TABLE_ORDERS_PRODUCTS, $sql_data_array, 'update', 'orders_products_id = \''.$_POST['opID'].'\'');

		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=options&oID='.$_POST['oID'].'&pID='.$products->fields['products_id'].'&opID='.$_POST['opID']));
		break;

	case 'ot_delete' : // Löschen eines OT Moduls aus der Bestellung
		$db->db_query("DELETE FROM ".TABLE_ORDERS_TOTAL." WHERE orders_total_id = '".$_POST['otID']."'");
		redirect(href_link(FILENAME_ORDERS_EDIT, 'edit_action=other&oID='.$_POST['oID']));
		break;
}

function calculate_tax($amount) {
	global $price, $db, $status;
	
	$Tprice = 'b_price';
	if($status->fields['customers_status_show_price_tax'] == 0 && $status->fields['customers_status_add_tax_ot'] == 1)
		$Tprice = 'n_price';
	
	$sum_total = $db->db_query("select SUM(".$Tprice.") as price from ".TABLE_ORDERS_RECALCULATE." where orders_id = '".(int)$_POST['oID']."' and class = 'products'");
	
	//Gutscheinwert/Rabatt in % berechnen, vereinheitlicht die Berechnungen
	if($sum_total->fields['price'] == 0) 
		return 0;
	$amount_pro = $amount/$sum_total->fields['price'] * 100;
	
	//Steuersätze alle Produkte der Bestellung feststellen
	$tax_rate = $db->db_query("select tax_rate from ".TABLE_ORDERS_RECALCULATE." where orders_id = '".(int)$_POST['oID']."' and class = 'ot_tax' GROUP BY tax_rate");
	
	$tod_amount = 0;
	//Berechnungen pro Steuersatz durchführen
	while (!$tax_rate->EOF) {
		$tax_total = $db->db_query("select SUM(tax) as value from ".TABLE_ORDERS_RECALCULATE." where orders_id = '".(int)$_POST['oID']."' and tax_rate = '". $tax_rate->fields['tax_rate']."'and class = 'products'");
		
		$new_tax_total = $tax_total->fields['value'] * $amount_pro / 100;
		
		$new_tax = $db->db_query("select tax as value from ".TABLE_ORDERS_RECALCULATE." where orders_id = '".(int)$_POST['oID']."' and tax_rate = '". $tax_rate->fields['tax_rate']."'and class = 'ot_tax'");

		$new_tax = $new_tax_total->fields['value'] + $god_amount;

		$db->db_query("UPDATE ".TABLE_ORDERS_RECALCULATE."
		                 SET tax = '".$new_tax."'
		               WHERE orders_id = '".(int)$_POST['oID']."'
		                 AND tax_rate = '".$tax_rate->fields['tax_rate']."'
		                 AND class = 'ot_tax'");

		$tod_amount += $god_amount;

		$tax_rate->MoveNext();
	}
	
	return $tod_amount;
}

function get_c_infos($customers_id, $delivery_country_iso_code_2) {
	global $db;
	
	$countries = $db->db_query("select 
									c.countries_id
								from 
									".TABLE_COUNTRIES." c
								where 
									c.countries_iso_code_2  = '".$delivery_country_iso_code_2."'");
	
	$zone_id = '';
	if($countries->fields['countries_id'] > 0) {
		$zones = $db->db_query("select 
									z.zone_id
								from 
									".TABLE_ORDERS." o,
									".TABLE_ZONES." z
								where 
									o.customers_id  = '".$customers_id."'
								and 
									z.zone_country_id = '".$countries->fields['countries_id']."'
								and 
									z.zone_name = o.delivery_state");

		$zone_id = $zones->fields['zone_id'];
	}
	
	$c_info_array = array('country_id' => $countries->fields['countries_id'],
	                    'zone_id' => $zone_id);
	
	return $c_info_array;
}

require(DIR_WS_INCLUDES.'metatag.php');
?>
</style>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
	<table class="outerTable" cellspacing="0" cellpadding="0">
		<tr>
			<td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
				<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
			</td>
			<td valign="top" class="centerBox">
				<table border="0" width="100%" cellspacing="0" cellpadding="0" class="boxCenter">
					<tr>
						<td valign="top">
							<table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
								<tr>
									<td class="pageHeading"><?php echo TABLE_HEADING;?></td>
								</tr>
							</table>
							<?php
							if($_GET['text'] == 'address') { ?>
								<table border="0" width="100%" cellspacing="0" cellpadding="2">
									<tr>
										<td class="main">
										<b>
											<?php
												echo TEXT_EDIT_ADDRESS_SUCCESS;
											?>
										</b>
										</td>
									</tr>
								</table>
							<?php
							}
							if($_GET['edit_action'] == 'address')
								include ('orders_edit_address.php');
							
							elseif($_GET['edit_action'] == 'products')
								include ('orders_edit_products.php');
							
							elseif($_GET['edit_action'] == 'other')
								include ('orders_edit_other.php');
							
							elseif($_GET['edit_action'] == 'options')
								include ('orders_edit_options.php');
							
							?>
					</td>
					<?php
					$order = new order($_GET['oID']);
					$heading = array ();
					$contents = array ();
					switch ($_GET['action']) {
						default :
							if(is_object($order)) {
								$heading[] = array ('text' => '<b>'.TABLE_HEADING_ORDER.$_GET['oID'].'</b>');
	
								$contents[] = array ('align' => 'center', 'text' => '<br />'.TEXT_EDIT_ADDRESS.'<br /><a class="button" onClick="this.blur();" href="'.href_link(FILENAME_ORDERS_EDIT, 'edit_action=address&oID='.$_GET['oID']).'">'.BUTTON_EDIT.'</a><br /><br />');
								$contents[] = array ('align' => 'center', 'text' => '<br />'.TEXT_EDIT_PRODUCTS.'<br /><a class="button" onClick="this.blur();" href="'.href_link(FILENAME_ORDERS_EDIT, 'edit_action=products&oID='.$_GET['oID']).'">'.BUTTON_EDIT.'</a><br /><br />');
								$contents[] = array ('align' => 'center', 'text' => '<br />'.TEXT_EDIT_OTHER.'<br /><a class="button" onClick="this.blur();" href="'.href_link(FILENAME_ORDERS_EDIT, 'edit_action=other&oID='.$_GET['oID']).'">'.BUTTON_EDIT.'</a><br /><br />');
	
							}
							break;
					}

					if((not_null($heading)) && (not_null($contents))) {
						echo '<td width="20%" valign="top" class="border">'."\n";
						$box = new box;
						echo $box->infoBox($heading, $contents);
						echo '</td>'."\n";
					}
					?>
				</tr>
			</table>
			<table border="0" width="100%" cellspacing="0">
				<tr class="dataTableRow">
					<td class="dataTableContent" align="right">
					<?php
						echo TEXT_SAVE_ORDER;
						echo draw_form('save_order', FILENAME_ORDERS_EDIT, 'action=save_order', 'post');
						echo draw_hidden_field('customers_status_id', $address[customers_status]);
						echo draw_hidden_field('oID', $_GET['oID']);
						echo draw_hidden_field('cID', $_GET['cID']);
						echo '<button type="submit" class="button" onClick="this.blur();">'.BUTTON_SAVE.'</button>';
						echo '</form>';
					?>
					</td>
				</tr>
			</table>
		</td>
		</tr>
	</table>
 </div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php');